<?php


namespace app\controller\api;


use app\BaseController;
use app\common\exception\ApiException;
use app\model\user\UserModel;
use app\service\utils\GenerateUserNickService;
use app\service\utils\WeiXinService;
use think\facade\Log;

class WxController extends BaseController
{

    /**
     * 获取手机号
     * @return \think\response\Json
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getPhoneNumber()
    {
        try {
            Log::info('获取手机号');
            $code = $this->request->param('code', '', 'trim');
            if (!$code) {
                throw new ApiException('缺少参数');
            }
            $weiXinService = new WeiXinService();
            $result = $weiXinService->getMobile($code);
            return $this->success($result);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 获取openid
     * @return \think\response\Json
     */
    public function getOpenId()
    {
        try {
            Log::info('获取openid');
            $code = $this->request->param('code', '', 'trim');
            if (!$code) {
                throw new ApiException('缺少参数');
            }
            $weiXinService = new WeiXinService();
            $result = $weiXinService->wxAuth($code);
            $openId = $result['openid'];
            Log::info('获取openid：' . var_export($result, true));
            return $this->success($openId);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 微信授权
     * 文档地址：https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
     * @return \think\response\Json
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function wxLogin()
    {
        try {
            Log::info('授权登录信息:' . var_export($this->request->param(), true));
            $openId = $this->request->param('openid', '', 'trim');
            $phone = $this->request->param('phone', '', 'trim');
            if (!$openId) {
                throw new ApiException('未获取到用户信息');
            }
            $token = password_hash($openId, PASSWORD_DEFAULT);

            $userInfo = UserModel::where('mobile', $phone)->find();
            if ($userInfo) {
                $userInfo->token = $token;
                $userInfo->openid = $openId;
                $userInfo->save();
                return $this->success(['token' => $token]);
            }
            // 邀请人
            $scene = $this->request->param('scene', 0, 'int');
            $type = $this->request->param('type', 0, 'int');

            $userModel = new UserModel();
            $userModel->mobile = $phone;
            $userModel->openid = $openId;
            $userModel->token = $token;
            $userModel->username = GenerateUserNickService::generateUserNick();
            if ($type == 0) {
                $userModel->pid = $scene;
                $userModel->share_type = 0;
            } else if ($type == 1) {
                $userModel->coach_id = $scene;
                $userModel->share_type = 1;
            } else {
                $userModel->partner_id = $scene;
                $userModel->share_type = 2;
            }
            $userModel->save();
            return $this->success(['token' => $token]);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    public function echoStr()
    {
        echo $this->request->param('echostr');
        return;
    }
}